package com.cmcc.cmii.smspush.wechat.rest.wechat;

import com.cmcc.cmii.common.enums.DeleteFlagEnum;
import com.cmcc.cmii.common.result.ResultVo;
import com.cmcc.cmii.common.result.ResultVoUtils;
import com.cmcc.cmii.smspush.model.wechat.WechatBaseModel;
import com.cmcc.cmii.smspush.wechat.entity.PushWechatInfo;
import com.cmcc.cmii.smspush.wechat.service.wechat.IPushWechatInfoService;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Thanks
 */
@RestController
@RequestMapping("/wechat")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class WechatApiController {

    private final WxMpService wxMpService;

    private final IPushWechatInfoService iPushWechatInfoService;


    @PostMapping("/getUserList")
    public ResultVo getUserList(@RequestBody WechatBaseModel wechatBaseModel) throws Exception{
        PushWechatInfo pushWechatInfo = new PushWechatInfo();
        pushWechatInfo.setSysId(wechatBaseModel.getSysId());
        pushWechatInfo.setType(wechatBaseModel.getType());
        pushWechatInfo.setDeleteFlag(DeleteFlagEnum.NORMAL.getCode());
        pushWechatInfo = iPushWechatInfoService.queryOne(pushWechatInfo);
        WxMpDefaultConfigImpl wxStorage = new WxMpDefaultConfigImpl();
        wxStorage.setAppId(pushWechatInfo.getAppId());
        wxStorage.setSecret(pushWechatInfo.getAppSecret());
        wxMpService.setWxMpConfigStorage(wxStorage);
        WxMpUserService userService = wxMpService.getUserService();
        WxMpUserList wxMpUserList = userService.userList(null);
        List<WxMpUser> userList = getWxMpUserList(userService, wxMpUserList.getOpenids());
        return ResultVoUtils.success(userList);
    }



    /**
     * 分批次获取微信粉丝信息 每批100条
     * @param wxMpUserService
     * @param openidsList
     * @return
     * @throws WxErrorException
     * @author
     */
    @SuppressWarnings("all")
    private static List<WxMpUser> getWxMpUserList(WxMpUserService wxMpUserService, List<String> openidsList) throws WxErrorException {
        // 粉丝openid数量
        int count = openidsList.size();
        if (count <= 0) {
            return new ArrayList<>();
        }
        List<WxMpUser> list = Lists.newArrayList();
        List<WxMpUser> followersInfoList;
        int a = count % 100 > 0 ? count / 100 + 1 : count / 100;
        for (int i = 0; i < a; i++) {
            if (i + 1 < a) {
                log.debug("i:{},from:{},to:{}", i, i * 100, (i + 1) * 100);
                followersInfoList = wxMpUserService.userInfoList(openidsList.subList(i * 100, ((i + 1) * 100)));
                if (null != followersInfoList && !followersInfoList.isEmpty()) {
                    list.addAll(followersInfoList);
                }
            } else {
                log.debug("i:{},from:{},to:{}", i, i * 100, count - i * 100);
                followersInfoList = wxMpUserService.userInfoList(openidsList.subList(i * 100, count));
                if (null != followersInfoList && !followersInfoList.isEmpty()) {
                    list.addAll(followersInfoList);
                }
            }
        }
        log.debug("本批次获取微信粉丝数：",list.size());
        return list;
    }


}
